﻿using GoShopping.Model;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GoShopping.BLL.SysInfo
{
    /// <summary>
    /// 礼品卡操作类
    /// </summary>
    public class GiftCardBean
    {
        /// <summary>
        /// 创建一个礼品卡
        /// 
        /// IsUseOut, false
        /// </summary>
        /// <param name="gc"></param>
        /// <returns></returns>
        public int CreateOneGiftCard(GiftCard gc)
        {
            string uuid = Guid.NewGuid().ToString();
            return Databases.DBGoShopping.Insert(GoShoppingNBear.GiftCard).AddColumn(
                GoShoppingNBear.GiftCard.BoundUserID, gc.BoundUserID).AddColumn(
                GoShoppingNBear.GiftCard.BoundUserName, gc.BoundUserName).AddColumn(
                GoShoppingNBear.GiftCard.CardPrice, gc.CardPrice).AddColumn(
                GoShoppingNBear.GiftCard.CreateDate, DateTime.Now).AddColumn(
                GoShoppingNBear.GiftCard.Enable, gc.Enable).AddColumn(
                GoShoppingNBear.GiftCard.IsBound2User, gc.IsBound2User).AddColumn(
                GoShoppingNBear.GiftCard.IsUseOut, false).AddColumn(
                GoShoppingNBear.GiftCard.UUID, uuid).ExecuteReturnAutoIncrementID(
                GoShoppingNBear.GiftCard.ID);

        }

        public IList<GiftCard> GetAll()
        {
            return Databases.DBGoShopping.Select(
                GoShoppingNBear.GiftCard).ToList<GiftCard>();
        }

        /// <summary>
        /// 查询一个礼品卡
        /// </summary>
        /// <param name="giftCardUUID">礼品卡编号</param>
        /// <returns></returns>
        public GiftCard GetGiftCard(string giftCardUUID)
        {
            return Databases.DBGoShopping.Select(
                GoShoppingNBear.GiftCard).Where(
                GoShoppingNBear.GiftCard.UUID == giftCardUUID).ToSingleObject<GiftCard>();
        }

        /// <summary>
        /// 查询一个用户的礼品卡
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public IList<GiftCard> GetUsersGiftCard(string username)
        {
            return Databases.DBGoShopping.Select(
                GoShoppingNBear.GiftCard).Where(
                GoShoppingNBear.GiftCard.BoundUserName == username).ToList<GiftCard>();
        }

        /// <summary>
        /// 获取礼品卡的每次使用情况
        /// </summary>
        /// <param name="giftcardid"></param>
        /// <returns></returns>
        public IList<GiftCardUseContent> GetGiftCardContent(int giftcardid)
        {
            return Databases.DBGoShopping.Select(GoShoppingNBear.GiftCardUseContent).Where(
                GoShoppingNBear.GiftCardUseContent.GiftCardID == giftcardid).ToList<GiftCardUseContent>();
        }

        /// <summary>
        /// 查询礼品卡余额
        /// </summary>
        /// <param name="giftcardid"></param>
        /// <returns></returns>
        public decimal GetGiftCardBalance(int giftcardid)
        {
            decimal total = Databases.DBGoShopping.Select(
                GoShoppingNBear.GiftCard, GoShoppingNBear.GiftCard.CardPrice).Where(
                GoShoppingNBear.GiftCard.ID == giftcardid).ToScalar<decimal>();
            decimal use = Databases.DBGoShopping.Select(
                GoShoppingNBear.GiftCardUseContent, GoShoppingNBear.GiftCardUseContent.UsePrice.Sum()).Where(
                GoShoppingNBear.GiftCardUseContent.GiftCardID == giftcardid).ToScalar<decimal>();
            return total - use;
        }

        /// <summary>
        /// 添加一次礼品卡的使用
        /// 如果礼品卡金额用没，则更新礼品卡主表IsUseOut属性
        /// </summary>
        /// <param name="gcuc"></param>
        /// <returns></returns>
        public int AddOnceUseGiftCard(GiftCardUseContent gcuc)
        {
            decimal balance = this.GetGiftCardBalance(Convert.ToInt32(gcuc.GiftCardID));
            if (balance <= 0 && gcuc.UsePrice > balance)
            {
                throw new Exception("礼品卡中已经没有这么多金额");
            }
            int id = -1;
            using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required))
            {
                id = Databases.DBGoShopping.Insert(GoShoppingNBear.GiftCardUseContent).AddColumn(
                    GoShoppingNBear.GiftCardUseContent.GiftCardID, gcuc.GiftCardID).AddColumn(
                    GoShoppingNBear.GiftCardUseContent.UseDate, DateTime.Now).AddColumn(
                    GoShoppingNBear.GiftCardUseContent.UsePrice, gcuc.UsePrice).ExecuteReturnAutoIncrementID(
                    GoShoppingNBear.GiftCardUseContent.ID);
                if ((decimal)(balance - (decimal)gcuc.UsePrice) < (decimal)0.1)
                {
                    Databases.DBGoShopping.Update(
                        GoShoppingNBear.GiftCard).AddColumn(
                        GoShoppingNBear.GiftCard.IsUseOut, true).Execute();
                }
                scope.Complete();
            }
            gcuc.ID = id;
            return id;
        }

    }
}
